import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:qianyi/entity/home/ImageVideoDetailEntity.dart';
import 'package:qianyi/network/http_entity.dart';
import 'package:qianyi/network/http_manager.dart';
import 'package:video_player/video_player.dart';

import '../network/http_config.dart';
import '../state/token.dart';
import 'VideoPlayerController.dart';

class ImageDetailVideoController extends GetxController {
  Rx<String> imageDetailId = "".obs;
  RxList<ImageVideoDetailEntity> imageVideoDetailEntity = RxList();
  RxBool isAgree = false.obs;
  TokenController tokenController = Get.find<TokenController>();

  Future<void> getImageVideoDetail() async {
    this.imageVideoDetailEntity.clear();
    Map<String,String> params = {
      "id": imageDetailId.value
    };
    HttpEntity httpEntity = await HttpManager.getInstance().request(url: '/platform/imgDetail/getOne',method: Method.get,params: params) as HttpEntity;


    ImageVideoDetailEntity imageVideoDetailEntity = ImageVideoDetailEntity.fromJson(httpEntity.getOriData());
    String ava = imageVideoDetailEntity!.avatar!;
    Map<String,String> avap = {
      "path":ava
    };
    HttpEntity h = await HttpManager.getInstance().request(url: '/util/fileoss/getFileUrl',method: Method.get,params: avap) as HttpEntity;
    String avatar1 = h.getOriData()["data"];
    imageVideoDetailEntity.setAvatar(avatar1);
    String uid = await tokenController.getUid();
    Map<String,dynamic> paramss = {
      "uid":uid,
      "type":1,
      "agreeCollectId":imageVideoDetailEntity.id,
      "agreeCollectUid":imageVideoDetailEntity.userId
    };
    HttpEntity h1;
    h1 = await HttpManager.getInstance().request(url: '/platform/agreeCollect/isAgree',method: Method.post,params: paramss) as HttpEntity;

    isAgree.value = h1.getOriData()['data'];
    this.imageVideoDetailEntity.add(imageVideoDetailEntity);

  }



  RxList<VideoPlayerController> videoPlayerControllers = RxList<VideoPlayerController>(); // 创建一个视频播放器列表

  Future<void> initVideoPlayer1(List<String> videoUrls) async {
    videoPlayerControllers.clear(); // 清除之前的视频播放器
    for (int i = 0; i < videoUrls.length; i++) {
      VideoPlayerController videoPlayerController = VideoPlayerController.network(videoUrls[i]);
      videoPlayerControllers.add(videoPlayerController); // 将视频播放器添加到列表中

      await videoPlayerController.initialize();
      videoPlayerController.setLooping(true);
    }
  }

  void disposeVideoPlayers() {
    for (VideoPlayerController controller in videoPlayerControllers) {
      controller.pause();
      controller.dispose(); // 释放每个视频播放器的资源
    }
    videoPlayerControllers.clear(); // 清除视频播放器列表
  }

}